package com.example.client_anna.util.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.client_anna.R;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * @author cy
 * @email 272884043@qq.com
 * @date 2021/10/19
 */
public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ViewHolder> {

    private List<String> mList;
    private Context mContext;

    //viewholder与xml布局中的控件关联
    static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView imageImage;
        public ViewHolder(View view){
            super(view);
             imageImage = (ImageView) view.findViewById(R.id.img);
        }
    }

    //为适配器传入数据
    public ImageAdapter(List<String> list){
        mList = list;
    }

    @NonNull
    @NotNull
    @Override
    public ImageAdapter.ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        if (mContext == null){
            mContext = parent.getContext();
        }
        View view = LayoutInflater.from(mContext).inflate(R.layout.home_item_image,parent,false);
        ViewHolder holder = new ViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull ImageAdapter.ViewHolder holder, int position) {
        String url = mList.get(position);
        Glide.with(mContext)
                .load(url)
                .into(holder.imageImage);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

}
